/*!
 Temelia - Treap tree samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/treap_tree.h>

static void sample1()
{
	int **x, i, N = 100;
	treap_tree_t treap;

	PRINT("\n\n Sample 1 started \n\n");

	x = int_array_new(N, N);
	treap = treap_tree_new(x[0]);

	for (i = 0; i < N; i++)
		treap_tree_delete_node(treap_tree_new(NULL));

	PRINT("Compare %d %d\n",
			treap_tree_compare_trees(treap, treap, int_compare, NULL),
			treap_tree_compare_trees(treap, NULL, int_compare, NULL));
	PRINT("Empty %d %d, Leaf %d %d\n", treap_tree_is_empty(treap),
			treap_tree_is_empty(NULL), treap_tree_is_leaf(treap), treap_tree_is_leaf(
					NULL));

	for (i = 0; i < N; i++)
		treap_tree_insert(&treap, x[i], int_compare, NULL, 1);

	PRINT("Size = %d\n", treap_tree_get_size(treap));

	PRINT("\n Show indented:\n");
	treap_tree_show_indented(treap, int_level_handler, NULL);

	PRINT("\n Preorder :\n");
	treap_tree_preorder(treap, int_handler, NULL);

	PRINT("\n Inorder :\n");
	treap_tree_inorder(treap, int_handler, NULL);

	PRINT("\n Reverse inorder :\n");
	treap_tree_reverse_inorder(treap, int_handler, NULL);

	PRINT("\n Postorder :\n");
	treap_tree_postorder(treap, int_handler, NULL);

	PRINT("\n Level order :\n");
	treap_tree_level_order(treap, int_handler, NULL);

	PRINT("Root %p\n Key %p\n Priority %u\n Parent %p\n"
			" Left child %p\n Right child %p\n Height %d\n Depth %d\n",
			treap_tree_get_root(treap), treap_tree_get_key(treap),
			treap_tree_get_priority(treap), treap_tree_get_parent(treap),
			treap_tree_get_left_child(treap),
			treap_tree_get_right_child(treap), treap_tree_get_height(treap),
			treap_tree_get_depth(treap));

	PRINT("Min : %p; Max : %p\n", treap_tree_get_min(treap),
			treap_tree_get_max(treap));

	for (i = 0; i < N; i++)
		if (treap_tree_search(treap, x[i], int_compare, NULL) == NULL)
		{
			PRINT("Treap invalid result, see line %d\n", __LINE__);
			exit(1);
		}

	for (i = 0; i < N; i++)
		treap_tree_remove(&treap, x[i], int_compare, NULL, 1);

	treap_tree_delete(treap);

	int_array_delete(x, N);
	PRINT("\n\n Sample 1 ended \n\n");
}

void run_treap_tree_samples()
{
	sample1();
}
